package com.editso.haier.service;

import com.editso.haier.dao.CustomerDao;
import com.editso.haier.entity.Assets;
import com.editso.haier.entity.Customer;
import com.editso.haier.result.DetailsCustomer;
import com.editso.haier.result.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;

@Service
public class CustomerService {

    private CustomerDao customerDao;

    @Autowired
    public void setCustomer(CustomerDao customer) {
        this.customerDao = customer;
    }

    public Result createAssets(String name, String description){
        return Result.promise(callback->{
            Customer customer = new Customer();
            customer.setName(name);
            customer.setDescription(description);
            customer = this.customerDao.findByName(customer.getName()).orElse(customer);
            customerDao.save(customer);
        });
    }

    /**
     *
     * @param customerId 为空返回所有否则加载指定的资源
     */
    public Result getAssets(Integer customerId){
        return Result.promise(callback->{
            if (customerId == null){
                callback.accept(res->Result.success(
                        DetailsCustomer.from(customerDao.findAll(),false)
                ));
            }else{
                Customer customer = customerDao.findById(customerId).orElse(null);
                if (customer == null){
                    callback.reject(null);
                    callback.error(c->Result.failure("资源不存在"));
                }else {
                    callback.accept(r->Result.success(
                            DetailsCustomer.from(customer, true)
                    ));
                }
            }
        });
    }

    public Result deleteAssets(Integer cid, String aid){
        return Result.promise(callback->{
            Customer customer = customerDao.findById(cid).orElse(null);
            List<Assets> mAssets = customer.getAssets()
                    .stream()
                    .filter(assets -> assets.getId().equals(aid))
                    .collect(Collectors.toList());
            customer.getAssets().removeAll(mAssets);
            customerDao.save(customer);
        });
    }

    public Result updateAssets(){
        return null;
    }

    public Result appendAssets(Integer id, Map<String, Object> map) {
        return Result.promise(callback -> {
            Customer customer = customerDao.findById(id).orElse(null);
            if (customer == null){
                callback.error(e->Result.failure("资源不存在"));
            }else {
                List<String> assets = (List<String>) map.get("assets");
                Set<Assets> assetsSet = new HashSet<>();
                for (String asset : assets) {
                    assetsSet.add(new Assets(asset, "any", null));
                }
                customer.getAssets().addAll(assetsSet);
                customerDao.save(customer);
            }
        });
    }
}
